package com.snow.campuslink.secondHand.controller;


import com.snow.campuslink.project.result.Result;
import com.snow.campuslink.secondHand.converter.dto.UpdateSecondHandActionCountDto;
import com.snow.campuslink.secondHand.entity.SecondHandGoods;
import com.snow.campuslink.secondHand.service.ISecondHandGoodsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.snow.campuslink.utils.ParamCheckUtil.checkValidResult;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Gaosl
 * @since 2021-02-15
 */
@RestController
@RequestMapping("/secondHand/second-hand-goods")
public class SecondHandGoodsController {
    @Resource
    ISecondHandGoodsService secondHandGoodsService;

    @ApiOperation(value = "")
    @PostMapping("searchGoods")
    public Result searchGoods(){
        return Result.success();
    }

    @ApiOperation(value = "")
    @GetMapping("getGoods/{shGoodsId}")
    public Result getGoods(@PathVariable("shGoodsId") Long shGoodsId){
        SecondHandGoods byId = secondHandGoodsService.getById(shGoodsId);
        return Result.success().data(byId);
    }

    @ApiOperation(value = "")
    @PostMapping("releaseGoods")
    public Result releaseGoods(@RequestBody SecondHandGoods secondHandGoods){
        boolean save = secondHandGoodsService.save(secondHandGoods);
        if (!save) {
            return Result.fail();
        }
        return Result.success();
    }

    @ApiOperation("")
    @DeleteMapping("removeById/{shGoodsId}")
    public Result removeById (@PathVariable("shGoodsId") Long shGoodsId){
        boolean b = secondHandGoodsService.removeById(shGoodsId);
        if (!b) {
            return Result.fail();
        }
        return Result.success();
    }



    @ApiOperation("Feign接口")
    @PutMapping("updateActionCount")
    public void updateActionCount(@Validated @RequestBody UpdateSecondHandActionCountDto updateSecondHandActionCountDto, BindingResult result){
        checkValidResult(result);
        secondHandGoodsService.updateActionCount(updateSecondHandActionCountDto);
    }

}

